﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.Entity;
using School.Entities;
using School.Common;
using School.Accounting;

namespace School.Pages.Accounting
{
    public partial class Vouchers : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if ((IQueryable<int>)Session["privilage"] != null)
            {
                var x = (IQueryable<int>)Session["privilage"];

                if (x.Contains(119) == true)
                {

                    if (!IsPostBack)
                    {
                        LstCampuses.DataSource = Structure.GetCampuses();
                        LstCampuses.DataValueField = "Id";
                        LstCampuses.DataTextField = "Name";
                        LstCampuses.DataBind();

                        LstCertificates.DataSource = Structure.GetCertificatesByCampus(long.Parse(LstCampuses.SelectedValue));
                        LstCertificates.DataValueField = "Id";
                        LstCertificates.DataTextField = "Name";
                        LstCertificates.DataBind();

                        LstCertificates.Items.Insert(0, new ListItem() { Value = "0", Text = "اختر الشهادة" });

                        LstStages.DataSource = Structure.GetStagesByCertificate(long.Parse(LstCertificates.SelectedValue));
                        LstStages.DataValueField = "Id";
                        LstStages.DataTextField = "Name";
                        LstStages.DataBind();
                        LstStages.Items.Insert(0, new ListItem() { Value = "0", Text = "اختر المرحلة" });

                        LstGrades.DataSource = Structure.GetGradesByStage(long.Parse(LstStages.SelectedValue));
                        LstGrades.DataValueField = "Id";
                        LstGrades.DataTextField = "Name";
                        LstGrades.DataBind();
                        LstGrades.Items.Insert(0, new ListItem() { Value = "0", Text = "اختر الصف" });

                        LstStudents.DataSource = Students.GetStudentsByGrade(long.Parse(LstGrades.SelectedValue));
                        LstStudents.DataValueField = "Id";
                        LstStudents.DataTextField = "Name";
                        LstStudents.DataBind();
                        LstStudents.Items.Insert(0, new ListItem() { Value = "0", Text = "اختر الطالب" });
                    }
                }
                else
                    Response.Redirect("../organization/no_privelege.aspx");
            }
            else
                Response.Redirect("../organization/no_privelege.aspx");
        }

        protected void LstCampuses_SelectedIndexChanged(object sender, EventArgs e)
        {
            LstCertificates.DataSource = Structure.GetCertificatesByCampus(long.Parse(LstCampuses.SelectedValue));
            LstCertificates.DataValueField = "Id";
            LstCertificates.DataTextField = "Name";
            LstCertificates.DataBind();

            LstCertificates.Items.Insert(0, new ListItem() { Value = "0", Text = "اختر الشهادة" });

            LstStages.DataSource = Structure.GetStagesByCertificate(long.Parse(LstCertificates.SelectedValue));
            LstStages.DataValueField = "Id";
            LstStages.DataTextField = "Name";
            LstStages.DataBind();
            LstStages.Items.Insert(0, new ListItem() { Value = "0", Text = "اختر المرحلة" });

            LstGrades.DataSource = Structure.GetGradesByStage(long.Parse(LstStages.SelectedValue));
            LstGrades.DataValueField = "Id";
            LstGrades.DataTextField = "Name";
            LstGrades.DataBind();
            LstGrades.Items.Insert(0, new ListItem() { Value = "0", Text = "اختر الصف" });

            LstStudents.DataSource = Students.GetStudentsByGrade(long.Parse(LstGrades.SelectedValue));
            LstStudents.DataValueField = "Id";
            LstStudents.DataTextField = "Name";
            LstStudents.DataBind();
            LstStudents.Items.Insert(0, new ListItem() { Value = "0", Text = "اختر الطالب" });
        }

        protected void LstCertificates_SelectedIndexChanged(object sender, EventArgs e)
        {
            LstStages.DataSource = Structure.GetStagesByCertificate(long.Parse(LstCertificates.SelectedValue));
            LstStages.DataValueField = "Id";
            LstStages.DataTextField = "Name";
            LstStages.DataBind();
            LstStages.Items.Insert(0, new ListItem() { Value = "0", Text = "اختر المرحلة" });

            LstGrades.DataSource = Structure.GetGradesByStage(long.Parse(LstStages.SelectedValue));
            LstGrades.DataValueField = "Id";
            LstGrades.DataTextField = "Name";
            LstGrades.DataBind();
            LstGrades.Items.Insert(0, new ListItem() { Value = "0", Text = "اختر الصف" });

            LstStudents.DataSource = Students.GetStudentsByGrade(long.Parse(LstGrades.SelectedValue));
            LstStudents.DataValueField = "Id";
            LstStudents.DataTextField = "Name";
            LstStudents.DataBind();
            LstStudents.Items.Insert(0, new ListItem() { Value = "0", Text = "اختر الطالب" });
        }

        protected void LstStages_SelectedIndexChanged(object sender, EventArgs e)
        {
            LstGrades.DataSource = Structure.GetGradesByStage(long.Parse(LstStages.SelectedValue));
            LstGrades.DataValueField = "Id";
            LstGrades.DataTextField = "Name";
            LstGrades.DataBind();
            LstGrades.Items.Insert(0, new ListItem() { Value = "0", Text = "اختر الصف" });

            LstStudents.DataSource = Students.GetStudentsByGrade(long.Parse(LstGrades.SelectedValue));
            LstStudents.DataValueField = "Id";
            LstStudents.DataTextField = "Name";
            LstStudents.DataBind();
            LstStudents.Items.Insert(0, new ListItem() { Value = "0", Text = "اختر الطالب" });
        }

        protected void LstGrades_SelectedIndexChanged(object sender, EventArgs e)
        {
            LstStudents.DataSource = Students.GetStudentsByGrade(long.Parse(LstGrades.SelectedValue)).Select(x => new { x.Id, Name = x.Ar_FirstName + " " + x.Ar_SecondName + " " + x.Ar_ThirdNmae + " " + x.Ar_FourthName });
            LstStudents.DataValueField = "Id";
            LstStudents.DataTextField = "Name";
            LstStudents.DataBind();
            LstStudents.Items.Insert(0, new ListItem() { Value = "0", Text = "اختر الطالب" });
        }

        protected void LstStudents_SelectedIndexChanged(object sender, EventArgs e)
        {
        }

        protected void Button2_Click(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            Panel1.Visible = true;

            long number = 0;
            long student = 0;

            if (long.TryParse(TxtOrderNumber.Text, out number))
            {
                if (number > 0)
                {
                    var data = Voucher.GetPaymentOrdersByNumber(number);

                    if (data.Count() > 0)
                    {
                        GridView1.DataSource = data;
                        GridView1.DataBind();
                    }
                }
            }
            else if (long.TryParse(LstStudents.SelectedValue, out student) && number == 0)
            {
                if (student > 0)
                {
                    var data = Voucher.GetPaymentOrdersByStudent(student);

                    if (data.Count() > 0)
                    {
                        GridView1.DataSource = data;
                        GridView1.DataBind();
                    }
                }
            }

            if (GridView1.Rows.Count == 0)
            {
                LblNotFound.Visible = true;
            }

        }
    }
}